﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Comdiv.QWeb.Remoting;

namespace TestQWebRemotingConsole
{
    class Program
    {
        private static string url;
        private static string action;
        private static string render;
        private static string usr;
        private static string pass;
        static IDictionary<string ,string > parameters = new Dictionary<string, string>();

        static void Main(string[] args_) {
            var args = new List<string>(args_);
            if(args.Count==0) {
                var s = "";
                bool repeat = true;
                Console.Write("> ");
                while((s=Console.ReadLine())!="quit") {

                    if (s == "clear") {
                        parameters.Clear();
                        Console.Write("> ");
                        continue;

                    }
                    if(s=="current") {
                        renderCurrent();
                        Console.Write("> ");
                        continue;
                        
                    }
                    if (s == "")
                    {
                        
                        executeCommand(args,repeat);
                        args.Clear();
                        Console.WriteLine();
                        repeat = true;
                        Console.Write("> ");
                    }
                    else if (s.StartsWith("pass="))
                    {
                        Console.Clear();
                        pass = s.Split('=')[1];
                        Console.Write("> ");
                        continue;
                        ;
                    }
                    else if (s.StartsWith("usr="))
                    {
                        usr = s.Split('=')[1];
                        Console.Write("> ");
                        continue;
                        ;
                    }
                    else if (s.StartsWith("url="))
                    {
                        url = s.Split('=')[1];
                        Console.Write("> ");
                        continue;
                        ;
                    }
                    else if (s.StartsWith("action="))
                    {
                        action = s.Split('=')[1];
                        Console.Write("> ");
                        continue;
                        ;
                    }
                    else if (s.StartsWith("render="))
                    {
                        render = s.Split('=')[1];
                        Console.Write("> ");
                        continue;
                        ;
                    }
                    else {
                        
                        args.Add(s.Trim());
                        Console.Write("> ");
                    }
                }
                return;
            }


            executeCommand(args,false);
        }

        private static void renderCurrent() {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Url = {0}",url);
            Console.WriteLine("Action = {0}", action);
            Console.WriteLine("Render = {0}", render);
            Console.WriteLine("User = {0}", usr);
            if (!string.IsNullOrEmpty(pass)) {
                Console.WriteLine("Pass = ***********", pass);
            }
            Console.WriteLine("Parameters:");
            foreach (var p in parameters) {
                Console.WriteLine("{0} = {1}",p.Key,p.Value);
            }
            Console.ResetColor();
        }

        private static void executeCommand(List<string> args, bool repeat) {
            try {
            IDictionary<string, string> parameters = null;
            if(repeat) {
                parameters = prepareRepeat(args);
            }else {
                parameters = prepareNew(args);
            }
            _execute(parameters);
            }

            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("GENERAL ERROR");
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.ResetColor();
            }
        }

        private static IDictionary<string, string> prepareRepeat(List<string> args) {
            var pars = args;
            foreach (var par in pars)
            {
                var p = par.Split('=');
                if (p[0] == "user")
                {
                    usr = p[1];
                }
                else if (p[0] == "pass")
                {
                    pass = p[1];
                }
                else if(p[0] == "action") {
                    action = p[1];
                }
                else if (p[0] == "render")
                {
                    render = p[1];
                }
                else if (p[0] == "url")
                {
                    url = p[1];
                }
                else {
                    parameters[p[0]] = p[1];
                }
            }
            return parameters;
        }


        private static IDictionary<string, string> prepareNew(List<string> args) {
            url = args[0];
            action = args[1];
            render = args[2];
            var pars = args.Skip(3);
            usr = "";
            pass = "";
            foreach (var par in pars) {
                var p = par.Split('=');
                if(p[0]=="user") {
                    usr = p[1];
                }else if(p[0]=="pass") {
                    pass = p[1];
                }
                parameters[p[0]] = p[1];
            }
            return parameters;
        }

        private static void _execute(IDictionary<string, string> parameters) {
            ICredentials cred = null;
            if(!string.IsNullOrWhiteSpace(usr)) {
                cred = new NetworkCredential(usr, pass);
            }
            var c = Connections.Create(url,cred);
            var query = new QWebQuery();
            query.Action = action;
            query.RenderName = render;
            query.Parameters = parameters;
            
                var result = c.Call(query);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Status : {0}", result.Status);
                Console.WriteLine("Lastmodified : {0}", result.LastModified);
                Console.WriteLine("ETag : {0}", result.ETag);
                Console.ResetColor();
                if (string.IsNullOrWhiteSpace(result.ErrorString)) {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(result.Content);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(result.ErrorString);
                }
                
           
        }
    }
}
